package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallUserGoods;
import org.linlinjava.litemall.db.service.LitemallUserGoodsService;
import org.linlinjava.litemall.wx.service.UserTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.wx.util.WxResponseCode.AUTH_INVALID_ACCOUNT;

@RestController
@RequestMapping("/wx/usergoods")
@Validated

public class WxUserGoodsController {
    private final Log logger = LogFactory.getLog(WxUserGoodsController.class);

    @Autowired
    private LitemallUserGoodsService goodsService;
    @PostMapping("login")
    public Object login(@RequestBody String body, HttpServletRequest request) {
        String username = JacksonUtil.parseString(body, "username");
        String password = JacksonUtil.parseString(body, "password");
        String shopname = JacksonUtil.parseString(body, "shopname");
        String address = JacksonUtil.parseString(body, "address");
        LitemallUserGoods goods = null;

        LitemallUserGoods userGoods = goodsService.queryUsername(username);

        if(userGoods == null){
            return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");
        }else{
            goods = goodsService.queryUserGoods(username);

            if (!goods.getPassword().equals(password)) {
                return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "密码输入错误");
            }
        }

        String token = UserTokenManager.generateToken(userGoods.getId());
        Map<Object, Object> map = new HashMap();
        map.put("username",username);
        map.put("token",token);
        map.put("code",200);
        return ResponseUtil.ok(map);
    }

    @PostMapping("regist")
    public Object usergoodsregist(@RequestBody LitemallUserGoods litemallUserGoods){
        int i = goodsService.insertUserGoods(litemallUserGoods);
        if(i == 1){
            return ResponseUtil.ok();
        }
       return ResponseUtil.fail();
    }

    @GetMapping("usergoodslist")
    public Object userGoodsList(){
        List<LitemallUserGoods> litemallUserGoods = goodsService.queryAll();
        return ResponseUtil.ok(litemallUserGoods);
    }

    @PostMapping("updateStat")
    public Object updateStat(@RequestBody LitemallUserGoods litemallUserGoods){
        int i = goodsService.updateStat(litemallUserGoods.getId(), litemallUserGoods.getDeleteId());
        return ResponseUtil.ok();
    }

    @DeleteMapping("deleteId")
    public Object deleteById(@RequestBody LitemallUserGoods litemallUserGoods){
        int i = goodsService.deleteById(litemallUserGoods.getId());
        return ResponseUtil.ok();
    }
}
